package controllers

import (
	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
	httpfacade "github.com/hulutech-web/http_result"
	"goravel/app/http/requests"
	"goravel/app/models"
	"goravel/app/services/sheet"
)

type ManhourController struct {
	//Dependent services
	sheet.SheetService
}

func NewManhourController() *ManhourController {
	return &ManhourController{
		//Inject services
	}
}

// Index 查询工时标准
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         ManhourController
// @Accept       json
// @Produce      json
// @Id ManhourIndex
// @Security ApiKeyAuth
// @Param manhourData body interface{} true "查询数据"
// @Param Authorization header string false "Bearer 用户令牌"
// @Success 200 {string} json {}
// @Router       /api/admin/manhour [get]
func (r *ManhourController) Index(ctx http.Context) http.Response {
	manhours := []models.Manhour{}
	queries := ctx.Request().Queries()
	res, err := httpfacade.NewResult(ctx).SearchByParams(queries, nil).
		ResultPagination(&manhours, "Kind", "Service")
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "", err.Error())
	}
	return res
}

// Store 查询工时标准
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         ManhourController
// @Accept       json
// @Produce      json
// @Id ManhourStore
// @Security ApiKeyAuth
// @Param manhourData body requests.ManhourRequest true "查询数据"
// @Param Authorization header string false "Bearer 用户令牌"
// @Success 200 {string} json {}
// @Router       /api/admin/manhour [post]
func (r *ManhourController) Store(ctx http.Context) http.Response {
	var request requests.ManhourRequest
	errors, err := ctx.Request().ValidateRequest(&request)
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "数据错误", err.Error())
	}
	if errors != nil {
		return httpfacade.NewResult(ctx).ValidError("", errors.All())
	}
	manhour := models.Manhour{}
	facades.Orm().Query().Model(&models.Manhour{}).
		Where("service_id=? ", request.ServiceID).Where("kind_id=?", request.KindID).Find(&manhour)
	if manhour.ID != 0 {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "该工时标准已存在", "该工时标准已存在")
	}
	facades.Orm().Query().Model(&models.Manhour{}).Create(&models.Manhour{
		ServiceID: request.ServiceID,
		KindID:    request.KindID,
		Score:     request.Score,
	})
	return httpfacade.NewResult(ctx).Success("创建成功", nil)
}

// Update 查询工时标准
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         ManhourController
// @Accept       json
// @Produce      json
// @Id ManhourUpdate
// @Security ApiKeyAuth
// @Param manhourData body requests.ManhourRequest true "查询数据"
// @Param Authorization header string false "Bearer 用户令牌"
// @Success 200 {string} json {}
// @Router       /api/admin/manhour/{id} [put]
func (r *ManhourController) Update(ctx http.Context) http.Response {
	id := ctx.Request().Route("id")
	hour := models.Manhour{}
	facades.Orm().Query().Model(&models.Manhour{}).Where("id=?", id).Find(&hour)
	var request requests.ManhourRequest
	errors, err := ctx.Request().ValidateRequest(&request)
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "数据错误", err.Error())
	}
	if errors != nil {
		return httpfacade.NewResult(ctx).ValidError("", errors.All())
	}
	hour.ServiceID = request.ServiceID
	hour.KindID = request.KindID
	hour.Score = request.Score
	facades.Orm().Query().Model(&models.Manhour{}).Where("id=?", id).Save(&hour)
	return httpfacade.NewResult(ctx).Success("修改成功", nil)
}

// Destroy 查询工时标准
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         ManhourController
// @Accept       json
// @Produce      json
// @Id ManhourDestroy
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Success 200 {string} json {}
// @Router       /api/admin/manhour/{id} [delete]
func (r *ManhourController) Destroy(ctx http.Context) http.Response {
	id := ctx.Request().Route("id")
	facades.Orm().Query().Model(&models.Manhour{}).Delete(&models.Manhour{}, id)
	return httpfacade.NewResult(ctx).Success("删除成功", nil)
}

// Init 用户分页查询，支持搜索，路由参数?name=xxx&pageSize=1&currentPage=1&sort=xxx&order=xxx,等其他任意的查询参数
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         ManhourController
// @Accept       json
// @Produce      json
// @Id ManhourInit
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Success 200 {string} json {}
// @Router       /api/admin/manhour/init [get]
func (r *ManhourController) Init(ctx http.Context) http.Response {
	r.FillRule("工时标准-格式化.xlsx")
	return httpfacade.NewResult(ctx).Success("初始化成功", nil)
}

// Query 查询工时标准
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         ManhourController
// @Accept       json
// @Produce      json
// @Id ManhourQuery
// @Security ApiKeyAuth
// @Param manhourData body interface{} true "查询数据"
// @Param Authorization header string false "Bearer 用户令牌"
// @Success 200 {string} json {}
// @Router       /api/admin/manhour/query [post]
func (r *ManhourController) Query(ctx http.Context) http.Response {
	service_ids := ctx.Request().Input("service_ids")
	kind_id := ctx.Request().Input("kind_id")
	manhour := []models.Manhour{}
	facades.Orm().Query().Model(&models.Manhour{}).Where("service_id = ? AND kind_id = ?", service_ids, kind_id).First(&manhour)
	return httpfacade.NewResult(ctx).Success("", manhour)
}

func (r *ManhourController) MiniQuery(ctx http.Context) http.Response {
	service_ids := ctx.Request().InputArray("service_ids")
	kind_id := ctx.Request().Input("kind_id")
	manhours := []models.Manhour{}
	facades.Orm().Query().Model(&models.Manhour{}).Where("service_id in ?", service_ids).Where("kind_id =?", kind_id).First(&manhours)
	return httpfacade.NewResult(ctx).Success("", manhours)
}
